function [D fname] = osl_convert_script(Sin)

% [D fname] = osl_convert_script(Sin)
% 
% Converts .fif file to continuous spm data file
%
% Mandatory Inputs:
%
% Sin.fif_file : file name of input fif file
% Sin.spm_file : file name of outputted spm file
%                
% Optional inputs:
%
% Sin.num_condition_trials_expected : number of expected trials for each condition 
% Sin.condition_code : list of condition trigger codes 
%
% MW

try; tmp=Sin.fif_file; catch, error('fif_file not specified');end;
try; tmp=Sin.spm_file; catch, error('spm_file not specified');end;
try; tmp=Sin.trigger_channel_mask; catch, Sin.trigger_channel_mask='0000000001111111';end;
%Sin.trigger_channel_mask='0000000000000111';

S = [];
S.dataset = Sin.fif_file;
S.outfile = Sin.spm_file;

[dir nam ext]=fileparts(Sin.fif_file);

if strcmp(ext,'.ds')
    %S.channels=ctf_chans;     % HL Mod 1.2 - CTF channel selection
    %TC 2013 the above works only for a 275 channel ctf system
    %To make more general, we want to read stim channels, the clock channel
    %and meg grads. Ideally data should actually be beamformed in 3rd gradient
    %which would require the reference channels but that would require
    %modification of the forward model (and you would also need to read in
    %balancing coefs). For now the data is read in unbalanced (raw) - this
    %does not take advantage of the noise corrections available to CTF
    %systems.
    S.channels='ctf_grads'    
    S.checkboundary = 0;
    convertfun = @spm_eeg_convert_v3; % TC 2013 I have created a v3 to read ctf sensors
    isctf=1;
else
    S.channels = 'all';
    S.checkboundary = 1;
    convertfun = @spm_eeg_convert_v2;
    isctf=0;
end



S.usetrials = 1;
S.datatype = 'float32-le';
S.eventpadding = 0;
S.saveorigheader = 0;
S.conditionlabel = {'Undefined'};
S.inputformat = [];
S.continuous = true;
S.fixoxfordneuromag=Sin.trigger_channel_mask;
D = feval(convertfun,S);

if ~isctf
eve=events(D,1);    

if(length(eve)>0)
    
for i=1:length(eve),                  
    vals(i)=eve(i).value;   
end;

bins=unique(vals); % HL mod 1.1
bins=[bins(1)-1 bins bins(end)+1]; % HL mod 1.1
h=hist(vals,bins); % HL mod 1.1
figure;bar(bins,h/2); % HL mod 1.1, correct by 2 due to up and down triggers
title('Histogram of events corrected for button presses');xlabel('Trigger codes');ylabel('# of triggers');

if(isfield(Sin,'num_condition_trials_expected'))
    if(~isempty(Sin.num_condition_trials_expected))

        % check to see if correct number of events have been found
        uvals=unique(vals);

            wrong=0;
        for i=1:length(Sin.condition_code),
            ind=find(uvals==Sin.condition_code(i));

            xs(i)=ind;
            ys(i)=h(ind);

            if(ys(i)~=Sin.num_condition_trials_expected(i))
                wrong=1;
            end;
        end;

        figure;bar(1:4, ys-Sin.num_condition_trials_expected);

        if(wrong)
           warning('Wrong number of events');
           print('-dpng',[working_dir '/' S.outfile '_num_trials']);

           h=hist(vals,unique(vals));
           figure;bar(unique(vals),h);

           print('-dpng',[working_dir '/' S.outfile '_num_trials2']);

        else
           disp('Correct number of events');
        end;
    end;
end;
    
else
    warning('No events detected');
end;

D=events(D,1,eve);
D.save;
end
fname=[D.path '/' D.fname];

end



% Subfunction containing CTF MEG channels - HL Mod 1.1 050312
function chans=ctf_chans275

chans= { 
    'UPPT002'
    'SCLK01'
    'MLC11'
    'MLC12'
    'MLC13'
    'MLC15'
    'MLC16'
    'MLC17'
    'MLC21'
    'MLC22'
    'MLC23'
    'MLC24'
    'MLC25'
    'MLC31'
    'MLC32'
    'MLC41'
    'MLC42'
    'MLC51'
    'MLC52'
    'MLC53'
    'MLC54'
    'MLC55'
    'MLC61'
    'MLC62'
    'MLC63'
    'MLF11'
    'MLF12'
    'MLF13'
    'MLF14'
    'MLF21'
    'MLF22'
    'MLF23'
    'MLF24'
    'MLF25'
    'MLF31'
    'MLF32'
    'MLF33'
    'MLF34'
    'MLF35'
    'MLF41'
    'MLF42'
    'MLF43'
    'MLF44'
    'MLF45'
    'MLF46'
    'MLF51'
    'MLF52'
    'MLF53'
    'MLF54'
    'MLF55'
    'MLF56'
    'MLF61'
    'MLF62'
    'MLF63'
    'MLF64'
    'MLF65'
    'MLF66'
    'MLF67'
    'MLO11'
    'MLO12'
    'MLO13'
    'MLO14'
    'MLO21'
    'MLO22'
    'MLO23'
    'MLO24'
    'MLO31'
    'MLO32'
    'MLO33'
    'MLO34'
    'MLO41'
    'MLO42'
    'MLO43'
    'MLO44'
    'MLO51'
    'MLO52'
    'MLO53'
    'MLP11'
    'MLP12'
    'MLP21'
    'MLP22'
    'MLP23'
    'MLP31'
    'MLP32'
    'MLP33'
    'MLP34'
    'MLP35'
    'MLP41'
    'MLP42'
    'MLP43'
    'MLP44'
    'MLP45'
    'MLP51'
    'MLP52'
    'MLP53'
    'MLP54'
    'MLP55'
    'MLP56'
    'MLP57'
    'MLT11'
    'MLT12'
    'MLT13'
    'MLT14'
    'MLT15'
    'MLT16'
    'MLT21'
    'MLT22'
    'MLT23'
    'MLT24'
    'MLT25'
    'MLT26'
    'MLT27'
    'MLT31'
    'MLT32'
    'MLT33'
    'MLT34'
    'MLT35'
    'MLT37'
    'MLT41'
    'MLT42'
    'MLT43'
    'MLT44'
    'MLT45'
    'MLT46'
    'MLT47'
    'MLT51'
    'MLT52'
    'MLT53'
    'MLT55'
    'MLT56'
    'MLT57'
    'MRC11'
    'MRC12'
    'MRC13'
    'MRC14'
    'MRC15'
    'MRC16'
    'MRC17'
    'MRC21'
    'MRC22'
    'MRC23'
    'MRC24'
    'MRC25'
    'MRC31'
    'MRC32'
    'MRC41'
    'MRC42'
    'MRC51'
    'MRC52'
    'MRC53'
    'MRC54'
    'MRC55'
    'MRC61'
    'MRC62'
    'MRC63'
    'MRF11'
    'MRF12'
    'MRF13'
    'MRF14'
    'MRF21'
    'MRF22'
    'MRF23'
    'MRF24'
    'MRF25'
    'MRF31'
    'MRF32'
    'MRF33'
    'MRF34'
    'MRF35'
    'MRF41'
    'MRF42'
    'MRF43'
    'MRF44'
    'MRF45'
    'MRF46'
    'MRF51'
    'MRF52'
    'MRF53'
    'MRF54'
    'MRF55'
    'MRF56'
    'MRF61'
    'MRF62'
    'MRF63'
    'MRF64'
    'MRF65'
    'MRF67'
    'MRO11'
    'MRO12'
    'MRO13'
    'MRO14'
    'MRO21'
    'MRO22'
    'MRO23'
    'MRO24'
    'MRO31'
    'MRO32'
    'MRO33'
    'MRO34'
    'MRO41'
    'MRO42'
    'MRO43'
    'MRO44'
    'MRO51'
    'MRO52'
    'MRO53'
    'MRP11'
    'MRP12'
    'MRP21'
    'MRP22'
    'MRP23'
    'MRP31'
    'MRP32'
    'MRP33'
    'MRP34'
    'MRP35'
    'MRP41'
    'MRP42'
    'MRP43'
    'MRP44'
    'MRP45'
    'MRP51'
    'MRP52'
    'MRP53'
    'MRP54'
    'MRP55'
    'MRP56'
    'MRP57'
    'MRT11'
    'MRT12'
    'MRT13'
    'MRT14'
    'MRT15'
    'MRT16'
    'MRT21'
    'MRT22'
    'MRT23'
    'MRT24'
    'MRT25'
    'MRT26'
    'MRT27'
    'MRT31'
    'MRT32'
    'MRT33'
    'MRT34'
    'MRT35'
    'MRT36'
    'MRT37'
    'MRT41'
    'MRT42'
    'MRT43'
    'MRT44'
    'MRT45'
    'MRT46'
    'MRT47'
    'MRT51'
    'MRT52'
    'MRT53'
    'MRT54'
    'MRT55'
    'MRT56'
    'MRT57'
    'MZC01'
    'MZC02'
    'MZC03'
    'MZC04'
    'MZF01'
    'MZF02'
    'MZF03'
    'MZO01'
    'MZO02'
    'MZO03'
    'MZP01'
    'UADC001'};

end

